package Entidades;

public class Fecha {

    private int dia;
    private int mes;
    private int any;
    
    public static final String MESES[] = {"enero", "febrero", "marzo", 
        "abril", "mayo", "junio", "julio", "agosto", "septiembre", 
        "octubre", "noviembre", "diciembre"};
    
    public Fecha() {
        dia = 1;
        mes = 1;
        any = 1970;
    }

    public Fecha(int dia, int mes, int any) {
        if (validarFecha(dia, mes, any)) {
            this.dia = dia;
            this.mes = mes;
            this.any = any;
        } else {
            this.dia = 1;
            this.mes = 1;
            this.any = 1970;
        }
    }

    public int getDia() {
        return dia;
    }

    public void setDia(int dia) {
        this.dia = dia;
    }

    public int getMes() {
        return mes;
    }

    public void setMes(int mes) {
        this.mes = mes;
    }

    public int getAny() {
        return any;
    }

    public void setAny(int any) {
        this.any = any;
    }

    public boolean validarFecha(int dia, int mes, int any) {
        return dia >=  1 && dia <= 31 &&
              (mes ==  1 || mes ==  3 || mes ==  5 ||
               mes ==  7 || mes ==  8 || mes == 10 ||
               mes == 12)                          ||
               dia >=  1 && dia <= 30 &&
              (mes ==  4 || mes ==  6 || mes ==  9 ||
               mes == 11)                          ||
               dia >=  1 && dia <= 29 && mes ==  2 &&
              (any % 400 == 0 || 
               any % 100 != 0 && any % 4 == 0)     ||
               dia >=  1 && dia <= 28 && mes ==  2;
    }
    
    public void mostrarFechaCorta() {
        System.out.printf("%02d/%02d/%d\n", dia, mes, any);
    }

    public void mostrarFechaLarga() {
        System.out.printf("%d de %s de %d\n", dia, MESES[mes - 1], any);
    }

    @Override
    public boolean equals(Object o) {
        Fecha f = (Fecha)o;
        return  this.dia == f.dia && 
                this.mes == f.mes && 
                this.any == f.any;
    }

    @Override
    public String toString() {
        return String.format("%02d/%02d/%4d", dia, mes, any);
    }

}
